#include "pathsongsview.h"
#include "ui_pathsongsview.h"
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QLabel>
#include <QDebug>
#include "normalplaylistmodel.h"
#include "songmodel.h"
#include "playEngine/ezvoiceplayermanager.h"
#include "common/songMenu/songmenu.h"

PathSongsView::PathSongsView(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PathSongsView)
{
    ui->setupUi(this);
    connect(ui->tableWidget,SIGNAL(cellDoubleClicked(int,int)),this,SLOT(cellDoubleClicked(int,int)));

    ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(ui->tableWidget,SIGNAL(customContextMenuRequested(const QPoint &)),this,SLOT(slot_customContextMenuRequested(const QPoint&)));
}

PathSongsView::~PathSongsView()
{
    delete ui;
}
QString PathSongsView::path() const
{
    return _path;
}

void PathSongsView::setPath(const QString &path)
{
    _path = path;
    reView();
}


void PathSongsView::reView()
{
    ui->tableWidget->clear();
    QDir dir(_path);
    QStringList nameFilter;
    nameFilter << "*.mP3" << "*.ape" << "*.flac" << "*.wav";
    dir.setNameFilters(nameFilter);
    dir.setFilter(QDir::Files);
    _fileNames = dir.entryList();
    ui->tableWidget->setRowCount(_fileNames.count());
    ui->tableWidget->setColumnCount(1);
    ui->tableWidget->setColumnWidth(0,ui->tableWidget->width());
    for(int i=0;i<_fileNames.count();i++){
        QLabel *label = new QLabel(ui->tableWidget);
        label->setText(_fileNames.at(i));
        ui->tableWidget->setCellWidget(i,0,label);
    }
}

void PathSongsView::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
//    ui->tableWidget->resizeColumnToContents(0);
    ui->tableWidget->setColumnWidth(0,ui->tableWidget->width());
}

void PathSongsView::cellDoubleClicked(int row, int)
{
    int index = row;

    EZVoicePlayerManager *playerManager = EZVoicePlayerManager::defaultManager();
    PlayListBaseModel *oldPlayLlist = playerManager->getPlayList();
    if(oldPlayLlist!=0){
        QVariant nowPlayPath = playerManager->getPlayList()->getUserData(QString("playListPath"));
        if(QString::compare(nowPlayPath.toString(),_path)==0){
            playerManager->playIndex(index);
            return;
        }
    }


    NormalPlayListModel *playList = new NormalPlayListModel();
    playList->setUserData(QString("playListPath"),QVariant(_path));
    for(int i=0;i<_fileNames.count();i++){
        QString songPath = _path +"/" + _fileNames.at(i);
        SongModel *song = new SongModel();
//        song->setFilePath(songPath);
        SongModel::Quality quality;
        quality.url = songPath;
        QString key(songHash_localPath);
        song->setQualityWithKey(key,quality);
        song->setTitle(_fileNames.at(i));
        playList->addSong(song);
        song->Release();
    }

    playerManager->setPlayList(playList);
    playerManager->playIndex(index);
    playList->Release();
}


void PathSongsView::slot_customContextMenuRequested(const QPoint & pos)
{
//    PathSongsView::
//    QTableWidgetItem* item = ui->tableWidget->itemAt(pos);
    int row = ui->tableWidget->rowAt(pos.y());
    if(row < 0){
        return;
    }

    QPoint point(pos.x(),ui->tableWidget->horizontalHeader()->height()+pos.y());

    QString songPath = _path +"/" + _fileNames.at(row);


    QMenu menu;
//    menu.addMenu()
    SongAddToMyPlayListMenu spm;

    QAction *addMenuAction = menu.addMenu(&spm);
    addMenuAction->setText("add to myPlayList");
    QAction *deleteAction = menu.addAction("delete song and file");

    QAction *selectAction = menu.exec(ui->tableWidget->mapToGlobal(point));
    if(selectAction==deleteAction){
        QFile::remove(songPath);
        reView();
    }else{
        SongModel *song = new SongModel();
        SongModel::Quality quality;
        quality.url = songPath;
        QString key(songHash_localPath);
        song->setQualityWithKey(key,quality);
        song->setTitle(_fileNames.at(row));

       spm.setSong(song);

        spm.runActionEvent(selectAction);

        song->Release();
    }

}
